//
//  MyViewController.m
//  UI14_豆瓣完整版
//
//  Created by dllo on 16/1/7.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "MyViewController.h"
#import "MyCollectionViewCell.h"
#import "MovieViewController1.h"
#import "MovieModel.h"

@interface MyViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, retain) NSMutableArray *modelArr;
@property (nonatomic, retain) NSMutableArray *tempArr;

@end

@implementation MyViewController

- (void)dealloc {
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor cyanColor];
    self.navigationController.navigationBar.translucent = NO;
    // 创建collectionView
    [self createCollectionView];
    // 数据处理
    [self dataHandler];
   
}

- (void)dataHandler {
    
    NSString *path = [[NSBundle mainBundle] pathForResource:@"movie" ofType:@"plist"];
    self.tempArr = [NSMutableArray arrayWithContentsOfFile:path];
    
    self.modelArr = [NSMutableArray array];
    
    for (NSDictionary *dic in _tempArr) {
        MovieModel *model = [MovieModel movieModel];
        [model setValuesForKeysWithDictionary:dic];
        [_modelArr addObject:model];
        
    }
}

- (void)createCollectionView {
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    layout.itemSize = CGSizeMake((375 - 40) / 3, 200);
    layout.minimumLineSpacing = 10;
    layout.minimumInteritemSpacing = 10;
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    
    
    UICollectionView *collection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64) collectionViewLayout:layout];
    collection.delegate = self;
    collection.dataSource = self;
    [self.view addSubview:collection];
    [collection release];
    [layout release];
    
    [collection registerClass:[MyCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    
}

// 返回item个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _modelArr.count;
}

// cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    MyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    
    MovieModel *model = _modelArr[indexPath.row];
    
    cell.model = model;
    
    return cell;
}

// 点击方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    MovieViewController1 *movVc = [[MovieViewController1 alloc] init];
    movVc.b = YES;
    [self.navigationController pushViewController:movVc animated:YES];
    
    NSDictionary *dic = _tempArr[indexPath.row];
    movVc.DIC = dic;
    
    [movVc release];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
